import MySQL
import Visualization


def var_hour():
    sql = 'SELECT * FROM hour_count'
    rows = MySQL.query(sql)
    c = {}
    maps = {'var': {'x': [], 'y': []}}
    date = 20160101
    while date <= 20160107:
        for hour in range(24):
            for row in rows:
                if row['date'] == str(date) and row['hour'] == hour:
                    c[row['group']] = row['count']
            avg = 0
            n = 0
            for k, v in c.items():
                n += 1
                avg += v
            avg = avg / n
            var = 0
            for k, v in c.items():
                var = var + (v - avg) * (v - avg)
            var = var / n
            maps['var']['x'].append('%02d' % (date % 100) + '_' + '%02d' % hour)
            maps['var']['y'].append(var)
        date += 1
    Visualization.draw(maps, chart_type='line', x_label='time line', y_label='var value', title='Variance Variety')
    return maps


def var_accumulation():
    sql = 'SELECT * FROM hour_count'
    rows = MySQL.query(sql)
    c = {}
    maps = {'var': {'x': [], 'y': []}}
    date = 20160101
    while date <= 20160107:
        for hour in range(24):
            for row in rows:
                if row['date'] == str(date) and row['hour'] == hour:
                    if row['group'] not in c:
                        c[row['group']] = 0
                    c[row['group']] += row['count']
            avg = 0
            n = 0
            for k, v in c.items():
                n += 1
                avg += v
            avg = avg / n
            var = 0
            for k, v in c.items():
                var = var + (v - avg) * (v - avg)
            var = var / n
            maps['var']['x'].append('%02d' % (date % 100) + '_' + '%02d' % hour)
            maps['var']['y'].append(var)
        date += 1
    Visualization.draw(maps, chart_type='line', x_label='time line', y_label='var value', title='Variance Variety')
    return maps


def var_hour_by_proportion():
    sql = 'SELECT * FROM hour_count'
    rows = MySQL.query(sql)
    c = {}
    maps = {'var': {'x': [], 'y': []}}
    date = 20160101
    while date <= 20160130:
        for hour in range(24):
            for row in rows:
                if row['date'] == str(date) and row['hour'] == hour:
                    c[row['group']] = row['count']
            sum_count = 0
            n = 0
            for k, v in c.items():
                n += 1
                sum_count += v
            avg = 1.0 / len(list(c.keys()))
            var = 0
            for k, v in c.items():
                var = var + (v / sum_count - avg) * (v / sum_count - avg)
            var = var / n
            maps['var']['x'].append('%02d' % (date % 100) + '_' + '%02d' % hour)
            maps['var']['y'].append(var)
        date += 1
    Visualization.draw(maps, chart_type='line', x_label='time line', y_label='var value', title='Variance Variety in 30 days')
    return maps


def var_accumulation_by_proportion():
    sql = 'SELECT * FROM hour_count'
    rows = MySQL.query(sql)
    c = {}
    maps = {'var': {'x': [], 'y': []}}
    date = 20160101
    while date <= 20160107:
        for hour in range(24):
            for row in rows:
                if row['date'] == str(date) and row['hour'] == hour:
                    if row['group'] not in c:
                        c[row['group']] = 0
                    c[row['group']] += row['count']
            sum_count = 0
            n = 0
            for k, v in c.items():
                n += 1
                sum_count += v
            avg = 1.0 / len(list(c.keys()))
            var = 0
            for k, v in c.items():
                var = var + (v / sum_count - avg) * (v / sum_count - avg)
            var = var / n
            maps['var']['x'].append('%02d' % (date % 100) + '_' + '%02d' % hour)
            maps['var']['y'].append(var)
        date += 1
    Visualization.draw(maps, chart_type='line', x_label='time line', y_label='var value', title='Variance Variety in 30 days')
    return maps


def var2_accumulation_by_proportion():
    sql = 'SELECT * FROM hour_count'
    rows = MySQL.query(sql)
    c = {}
    maps = {'var': {'x': [], 'y': []}}
    date = 20160101
    while date <= 20160130:
        for hour in range(24):
            for row in rows:
                if row['date'] == str(date) and row['hour'] == hour:
                    if row['group'] not in c:
                        c[row['group']] = 0
                    c[row['group']] += row['count']
            sum_count = 0
            for k, v in c.items():
                sum_count += v
            avg = 1.0 / len(list(c.keys()))
            var = 0
            for k, v in c.items():
                var = var + (v / sum_count - avg) * (v / sum_count - avg)
            var = var / len(list(c.keys()))
            maps['var']['x'].append('%02d' % (date % 100) + '_' + '%02d' % hour)
            maps['var']['y'].append(var)
        date += 1
    Visualization.draw(maps, chart_type='line', x_label='time line', y_label='var value', title='Variance Variety in 30 days')
    return maps


if __name__ == '__main__':
    MySQL.init('wiki')
    # var_accumulation()
    # var_hour()
    var_accumulation_by_proportion()
    # var_hour_by_proportion()